//
//  DetailViewController.m
//  BeanTrip
//
//  Created by dlios on 15-7-1.
//  Copyright (c) 2015年 PandaGroup. All rights reserved.
//

#import "DetailViewController.h"
#import "MBProgressHUD.h"
#import "DetailModel.h"
#import "DetailPicturesViewController.h"
#include "UIImageView+WebCache.h"
#import "TravelViewController.h"
#import "NoticeViewController.h"
#import "SiteTravelViewController.h"
#import "SiteModel.h"
#define WIDTH ([UIScreen mainScreen].bounds.size.width/375)
#define HEIGH ([UIScreen mainScreen].bounds.size.height/667)


@interface DetailViewController ()<UITableViewDataSource,UITableViewDelegate>

/**
 *  设置一个小菊花属性
 */
@property (nonatomic, retain) MBProgressHUD *HUD;
/**
 *  设置一个提示框属性
 */
@property(nonatomic, retain)UIAlertView *alter;

@property(nonatomic, retain)NSMutableArray *DetailArr;
/**
 *  图片上文字的显示
 */
@property(nonatomic, retain)UILabel *myLabel;
@property(nonatomic, retain)UILabel *myLabel1;

@property(nonatomic, retain)UIView *myView;
@property(nonatomic, retain)UIView *buttonView;
@property(nonatomic, retain)UIView *myButtonView;
@property(nonatomic, retain)UIImageView *myImage;
/**
 *  此button加载在图片上
 */
@property(nonatomic, retain)UIButton *myButton;
/**
 *  把tableView定义为属性
 */
@property(nonatomic, retain)UITableView *myTableView;
/**
 *  定义一个footView存放轮播图
 */
@property(nonatomic, retain)UIView *headerView;
/**
 *  存放图片的数组
 */
@property(nonatomic, retain)NSMutableArray *picArr;
/**
 *  旅行地点
 */
@property(nonatomic, retain)UIButton *siteButton;
@property(nonatomic, retain)UILabel *siteLabel;
/**
 *  精品游记
 */
@property(nonatomic, retain)UIButton *travelNotesButton;
@property(nonatomic, retain)UILabel *travelNotesLabel;
/**
 * 实用须知
 */
@property(nonatomic, retain)UIButton *noticeButton;
@property(nonatomic, retain)UILabel *noticeLabel;
/**
 *  轮播图
 */
@property(nonatomic, retain)UIScrollView *scrollView;

@property(nonatomic, retain)NSString *titleName;

@end

@implementation DetailViewController

- (void)dealloc
{
    [_HUD release];
    [_alter release];
    [_detailDic release];
    [_DetailArr release];
    [_myLabel release];
    [_myLabel1 release];
    [_myView release];
    [_buttonView release];
    [_myButtonView release];
    [_idString release];
    [_myButton release];
    [_typeNumber release];
    [_myImage release];
    [_myTableView release];
    [_headerView release];
    [_picArr release];
    [_siteButton release];
    [_siteLabel release];
    [_travelNotesButton release];
    [_travelNotesLabel release];
    [_noticeButton release];
    [_noticeLabel release];
    [_scrollView release];
    [_nameArr release];
    [_getTitle release];
    [_titleName release];
    [super dealloc];
}

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
      self.DetailArr=[NSMutableArray array];
      self.nameArr=[NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.translucent = NO;
    self.title = self.getTitle;
    self.view.backgroundColor=[UIColor colorWithRed:68/255.0 green:176/255.0 blue:193/255.0 alpha:1.0];
    self.myView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 375*WIDTH, (667*HEIGH-64)/3)];
    self.myView.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:self.myView];
    
    self.myImage=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 375*WIDTH, (667*HEIGH-64)/3)];
    [self.myView addSubview:self.myImage];
    
    self.myView.tag=1004;
    self.myLabel=[[UILabel alloc] initWithFrame:CGRectMake(20*WIDTH, (667*HEIGH-64)/3-70*HEIGH, 375*WIDTH/2-10*WIDTH, 40*HEIGH)];
    self.myLabel.textColor=[UIColor whiteColor];
    self.myLabel.font = [UIFont fontWithName:@"Helvetica-BoldOblique" size:17];
    
    self.myLabel1=[[UILabel alloc] initWithFrame:CGRectMake(20*WIDTH, (667*HEIGH-64)/3-30*HEIGH, 200*WIDTH, 20*HEIGH)];
    self.myLabel1.textColor=[UIColor whiteColor];
    self.myLabel1.font = [UIFont fontWithName:@"Helvetica-BoldOblique" size:12];
    [self.myView addSubview:self.myLabel];
    [self.myView addSubview:self.myLabel1];
    
    self.myButton=[UIButton buttonWithType:UIButtonTypeSystem];
    self.myButton.frame=CGRectMake(0, 0, 375*WIDTH, (667*HEIGH-64)/3);
    [self.myView addSubview:self.myButton];
    [self.myButton addTarget:self action:@selector(myClick:) forControlEvents:UIControlEventTouchUpInside];
    
    self.myButtonView=[[UIView alloc] initWithFrame:CGRectMake(0, 220*HEIGH, 375*WIDTH, (667*HEIGH-64)/3)];
  
    self.buttonView=[[UIView alloc] initWithFrame:CGRectMake(0, (667*HEIGH-64)/3, 375*WIDTH, 140*HEIGH)];
    self.buttonView.backgroundColor=[UIColor whiteColor];
    self.siteButton=[UIButton buttonWithType:UIButtonTypeCustom];
    self.siteButton.frame=CGRectMake(0, 20*HEIGH, 375*WIDTH/3, 50*HEIGH);
    [self.siteButton setImage:[UIImage imageNamed:@"景点.jpg"] forState:UIControlStateNormal];
    self.siteLabel=[[UILabel alloc] initWithFrame:CGRectMake(15*WIDTH, 80*HEIGH, 375*WIDTH/4, 20*HEIGH)];
    self.siteLabel.text=@"旅行景点";
    self.siteLabel.textAlignment=NSTextAlignmentCenter;
    self.siteLabel.font = [UIFont systemFontOfSize:15];
    self.siteLabel.textColor=[UIColor colorWithRed:88/255.0 green:200/255.0 blue:220/255.0 alpha:1.0];
    [self.siteButton addTarget:self action:@selector(siteClick:) forControlEvents:UIControlEventTouchUpInside];
    
    self.travelNotesButton=[UIButton buttonWithType:UIButtonTypeCustom];
    self.travelNotesButton.frame=CGRectMake(375*WIDTH/3, 20*HEIGH, 375*WIDTH/3, 50*HEIGH);
    [self.travelNotesButton setImage:[UIImage imageNamed:@"blue_mileagecup_20-1"] forState:UIControlStateNormal];
    self.travelNotesLabel=[[UILabel alloc] initWithFrame:CGRectMake(375*WIDTH/3, 80*HEIGH, 375*WIDTH/3, 20*WIDTH)];
    self.travelNotesLabel.text=@"精品游记";
    self.travelNotesLabel.textAlignment=NSTextAlignmentCenter;
    self.travelNotesLabel.font = [UIFont systemFontOfSize:15];
    self.travelNotesLabel.textColor=[UIColor colorWithRed:88/255.0 green:200/255.0 blue:220/255.0 alpha:1.0];
    [self.travelNotesButton addTarget:self action:@selector(travelNotesClick:) forControlEvents:UIControlEventTouchUpInside];
    
    self.noticeButton=[UIButton buttonWithType:UIButtonTypeCustom];
    self.noticeButton.frame=CGRectMake(375*WIDTH*2/3, 20*HEIGH, 375*WIDTH/3, 50*HEIGH);
    [self.noticeButton setImage:[UIImage imageNamed:@"destination_tag_other_selected-1"] forState:UIControlStateNormal];
    self.noticeLabel=[[UILabel alloc] initWithFrame:CGRectMake(375*WIDTH*2/3, 80*HEIGH, 375*WIDTH/3, 20*HEIGH)];
    self.noticeLabel.text=@"实用须知";
    self.noticeLabel.textAlignment=NSTextAlignmentCenter;
    self.noticeLabel.font = [UIFont systemFontOfSize:15];
    self.noticeLabel.textColor=[UIColor colorWithRed:88/255.0 green:200/255.0 blue:220/255.0 alpha:1.0];
    [self.noticeButton addTarget:self action:@selector(noticeClick:) forControlEvents:UIControlEventTouchUpInside];
    
    
    [self.buttonView addSubview:self.siteLabel];
    [self.buttonView addSubview:self.siteButton];
    [self.buttonView addSubview:self.travelNotesButton];
    [self.buttonView addSubview:self.travelNotesLabel];
    [self.buttonView addSubview:self.noticeButton];
    [self.buttonView addSubview:self.noticeLabel];
    
    UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(375*WIDTH/4, 100*HEIGH, 375*WIDTH/2, 40*HEIGH)];
    label.text=@"- - - 推荐城市 - - -";
    label.textColor=[UIColor blackColor];
    label.textAlignment=NSTextAlignmentCenter;
    label.font = [UIFont systemFontOfSize:18];
    
    UIButton *footButton=[UIButton buttonWithType:UIButtonTypeSystem];
    footButton.frame=CGRectMake(375*WIDTH/4, (667*HEIGH-64)/3+350*HEIGH, 375*WIDTH/2, 25*HEIGH);
    [footButton setTitle:@"全部热门地点" forState:UIControlStateNormal];
     footButton.tintColor=[UIColor blackColor];
    footButton.titleLabel.font=[UIFont systemFontOfSize:15];
    footButton.titleLabel.textAlignment=NSTextAlignmentCenter;
    footButton.layer.borderWidth=1;
    footButton.layer.cornerRadius=10;
    [self.view addSubview:self.buttonView];
    [self.buttonView addSubview:label];
    [self.view addSubview:footButton];
    [footButton addTarget:self action:@selector(footButtonClick:) forControlEvents:UIControlEventTouchUpInside];
  
    /**
     菊花
     
     :returns: return value description
     */
    self.HUD = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:self.HUD];
    self.HUD.labelText = @"加载中";
    [self.HUD show:YES];
    /**
     *执行createData方法
     */
    [self createData];
    [self createView];
    [self getData];
}

- (void)getData
{
     self.picArr = [NSMutableArray array];
    [HTTPTool get:[NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/%@/%@/pois/all/?sort=default&start=0&latitude=38.883740&longitude=121.544706&sign=8b29d40b1747a21dc6581e74e5a22fe4",self.typeNumber, self.idString] body:nil httpResult:JSON success:^(id result) {
           // 处理数据
        NSDictionary *dic = result;
        NSArray *itemsArr=dic[@"items"];
        for (NSDictionary *dicResult in itemsArr) {
            SiteModel *siteModel=[[SiteModel alloc] initWithDic:dicResult];
            [self.picArr addObject:siteModel.cover_s];
            [self.nameArr addObject:siteModel.name];
        }
         [self.myTableView reloadData];
    
        // 设置滚动范围
        self.scrollView.contentSize = CGSizeMake(375*WIDTH * self.picArr.count, 0);
        //在创建所有来显示的imageView之前,需要把之前的图片对象先移除
        for (NSInteger i=0; i<self.scrollView.subviews.count; i++) {
            UIImageView *imageView=self.scrollView.subviews[i];
            [imageView removeFromSuperview];
        }
        // 把图片添加到scrollView上
        for (NSInteger i = 0; i < self.picArr.count; i++) {
            // 创建imageview,用sdwebimage来解析图片
            UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(375*WIDTH * i, 0, 375*WIDTH, 200*HEIGH)];
            UILabel *nLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 10*HEIGH, 375*WIDTH, 30*HEIGH)];
            nLabel.textColor=[UIColor whiteColor];
            nLabel.font = [UIFont systemFontOfSize:17];
            nLabel.textAlignment=NSTextAlignmentCenter;
            nLabel.text = self.nameArr[i];
            [self.scrollView addSubview:image];
            [image addSubview:nLabel];
            [image sd_setImageWithURL:[NSURL URLWithString:self.picArr[i]]];
            
            /**
             *  占位图
             */
            NSURL *url = [NSURL URLWithString:self.picArr[i]];
            UIImage *imageV = [UIImage imageNamed:@"BeanTrip.png"];
            [image sd_setImageWithURL:url placeholderImage:imageV];
            
            
            
            
            [nLabel release];
            [image release];
          }
        }
        failure:^(NSError *error) {
            NSLog(@"%@",error);
        }];
}

- (void)createView
{
   self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, (667*HEIGH-64)/3+140*HEIGH, 375*WIDTH, 200*HEIGH)];
    [self.view addSubview:self.scrollView];
    [self.scrollView release];
    self.scrollView.backgroundColor=[UIColor colorWithRed:68/255.0 green:176/255.0 blue:193/255.0 alpha:1.0];
    // 设置偏移量
    self.scrollView.contentOffset = CGPointMake(375*WIDTH, 0);
    self.scrollView.pagingEnabled = YES;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    // 加一个定时器
    [NSTimer scheduledTimerWithTimeInterval:2.5 target:self selector:@selector(changeImage) userInfo:nil repeats:YES];
    self.scrollView.tag = 1000;
}
- (void)changeImage
{
    self.scrollView = (UIScrollView *)[self.view viewWithTag:1000];
    [self.scrollView setContentOffset:CGPointMake(self.scrollView.contentOffset.x + 375*WIDTH, 0) animated:YES];
    if (self.scrollView.contentOffset.x == 375*WIDTH * (self.picArr.count - 1)) {
        self.scrollView.contentOffset = CGPointMake(375*WIDTH, 0);
    }
}

-(void)createData{
  
    self.detailDic=[[NSMutableDictionary alloc] init];
    
        [HTTPTool get:[NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/%@/%@/",self.typeNumber, self.idString] body:nil httpResult:JSON success:^(id result) {
          NSDictionary *dic = result;
        NSArray *dataArr=dic[@"hottest_places"];
        for (NSDictionary *temp in dataArr) {
            DetailModel *cover = [[ DetailModel alloc] initWithDic:temp];
            [self.DetailArr addObject:cover];
        
            DetailModel *coverPic = self.DetailArr[0];
         
            [self.myImage sd_setImageWithURL:[NSURL                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             URLWithString:coverPic.photo]];
            
            /**
             *  占位图
             */
            NSURL *url = [NSURL URLWithString:coverPic.photo];
            UIImage *imageV = [UIImage imageNamed:@"BeanTrip.png"];
            [self.myImage sd_setImageWithURL:url placeholderImage:imageV];
            
            
            
            
            
        
        }
       self.titleName=dic[@"name_orig"];
        NSString *count=[NSString stringWithFormat: @"%@",dic[@"visited_count"]];
        NSString *count1=[NSString stringWithFormat: @"%@",dic[@"wish_to_go_count"]];
        self.myLabel.text = self.titleName;
        self.myLabel1.text =[NSString stringWithFormat:@"%@去过 / %@喜欢",count, count1];
        UIView *BYView=[self.myView viewWithTag:1004];
        [BYView reloadInputViews];
       /**
         *加载完成,取消菊花
         */
        [self.HUD removeFromSuperview];
        /**
         *  警告自动消失
         */
        [self.alter dismissWithClickedButtonIndex:0 animated:YES];
    }
    failure:^(NSError *error) {
            [self.HUD removeFromSuperview];
        self.alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"网络不给力" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [self.alter show];
        [self.alter release];
        NSLog(@"%@",error);
    }];
}

-(void)myClick:(UIButton *)button
{
    DetailPicturesViewController *DetailPic=[[DetailPicturesViewController alloc] init];
    DetailPic.getTypeNumber=self.typeNumber;
    DetailPic.getIdString=self.idString;
    DetailPic.titName = self.titleName;
    [self.navigationController pushViewController:DetailPic animated:YES];
}

-(void)siteClick:(UIButton *)button
{
    SiteTravelViewController *site=[[SiteTravelViewController alloc] init];
    [self.navigationController pushViewController:site animated:YES];
    site.getSiteType=self.typeNumber;
    site.getSiteId=self.idString;
    site.titleCon=self.titleName;
    [site release];
}

-(void)travelNotesClick:(UIButton *)button
{
    TravelViewController *travelVC=[[TravelViewController alloc] init];travelVC.getType=self.typeNumber;
        travelVC.getId=self.idString; 

    [self.navigationController pushViewController:travelVC animated:YES];
    [travelVC release];
}

-(void)noticeClick:(UIButton *)button
{
    NoticeViewController *noticeVC=[[NoticeViewController alloc] init];
    noticeVC.TId=self.idString;
    noticeVC.TType=self.typeNumber;
    [self.navigationController pushViewController:noticeVC animated:YES];
    [noticeVC release];
}

-(void)footButtonClick:(UIButton *)button
{
    SiteTravelViewController *siteVC=[[SiteTravelViewController alloc] init];
    [self.navigationController pushViewController:siteVC animated:YES];
    siteVC.getSiteId=self.idString;
    siteVC.getSiteType=self.typeNumber;
    [siteVC release];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
